﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace QuartzScheduler.ExpandClass
{
    public static class GetRequestInfo
    {
        public static string getCron(HttpRequestBase request)
        {
            List<string> monthList = new List<string>();//月份
            List<string> weekList = new List<string>();
            string weekCheck = request.Form["everyDayOrWeekRb"];
            string ischeckEndTimeChk = request.Form["ischeckEndTimeChk"];
            string TriggerTime = request.Form["TriggerTime"];
            string InfoText = request.Form["InfoText"];//工作的简介
            TimeSpan ts = TimeSpan.Parse(TriggerTime);
            //DateTime dt = DateTime.Parse(beginTime);
            #region 月份
            if (request.Form["January"] != "")
            {
                monthList.Add("1");
            }
            if (request.Form["February"] != "")
            {
                monthList.Add("2");
            }
            if (request.Form["March"] != "")
            {
                monthList.Add("3");
            }
            if (request.Form["April"] != "")
            {
                monthList.Add("4");
            }
            if (request.Form["May"] != "")
            {
                monthList.Add("5");
            }
            if (request.Form["June"] != "")
            {
                monthList.Add("6");
            }
            if (request.Form["July"] != "")
            {
                monthList.Add("7");
            }
            if (request.Form["August"] != "")
            {
                monthList.Add("8");
            }
            if (request.Form["September"] != "")
            {
                monthList.Add("9");
            }
            if (request.Form["October"] != "")
            {
                monthList.Add("10");
            }
            if (request.Form["November"] != "")
            {
                monthList.Add("11");
            }
            if (request.Form["December"] != "")
            {
                monthList.Add("12");
            }
            if (monthList.Count == 0)
            {
                for (int i = 0; i < 12; i++)
                {
                    monthList.Add((i+1).ToString());
                }
            }
            #endregion
            #region 星期
            if (request.Form["Monday"] != "")
            {
                weekList.Add("1");
            }
            if (request.Form["Tuesday"] != "")
            {
                weekList.Add("2");
            }
            if (request.Form["Wednesday"] != "")
            {
                weekList.Add("3");
            }
            if (request.Form["Thursday"] != "")
            {
                weekList.Add("4");
            }
            if (request.Form["Friday"] != "")
            {
                weekList.Add("5");
            }
            if (request.Form["Saturday"] != "")
            {
                weekList.Add("6");
            }
            if (request.Form["Sunday"] != "")
            {
                weekList.Add("7");
            }
            if (weekList.Count == 0)
            {
                for (int i = 0; i < 7; i++)
                {
                    weekList.Add((i + 1).ToString());
                }
            }
            #endregion
            string cronExpression = "";
            string SMH = string.Format("{0} {1} {2} ", ts.Seconds, ts.Minutes, ts.Hours);
            if (weekCheck == "天")
            {
                cronExpression = SMH + " * " + GetTriggerEveryMonth(monthList) + " ? ";
            }
            else
            {
                cronExpression = SMH + " ? " + GetTriggerEveryMonth(monthList) + " " + GetTriggerEveryWeek(weekList);
            }
            return cronExpression;
        }
        public static DateTime beginDateTime(HttpRequestBase request)
        {
            string beginTime = request.Form["beginTime"];
            return DateTime.Parse(beginTime).ToUniversalTime();
        }
        public static Dictionary<string, object> jobInfos(HttpRequestBase request)
        {
            Dictionary<string, object> jobInfo = new Dictionary<string, object>();
            jobInfo.Add("jobID", request.Form["jobID"]);
            jobInfo.Add("descriptionInfo", request.Form["InfoText"]);
            return jobInfo;
        }
        public static DateTime? endDateTime(HttpRequestBase request)
        {

            string endTime = request.Form["endTime"];
            string ischeckEndTimeChk = request.Form["ischeckEndTimeChk"];
            bool isSetEnd = false;
            if (ischeckEndTimeChk == ",on")
            {
                isSetEnd = true;
            }
            if (!isSetEnd)
            {
                return null;
            }
            if (endTime != null)
            {
                return DateTime.Parse(endTime).ToUniversalTime();
            }
            return null;
        }
        /// <summary>
        /// 获取工作名
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public static string getJobName(HttpRequestBase request)
        {
            return request.Form["jobName"];
        }
        public static string getJobClass(HttpRequestBase request)
        {
            return request.Form["jobClass"];
        }
        private static string GetTriggerEveryMonth(List<string> monthList)
        {
            string month = "";
            foreach (string s in monthList)
            {
                month += s + ",";
            }
            month = month.TrimEnd(',');
            if (month == "" || monthList.Count==12)
                month = "*";
            return month;
        }
        private static string GetTriggerEveryWeek(List<string> weekList)
        {
            string week = "";
            foreach (string s in weekList)
            {
                week += s + ",";
            }
            week = week.TrimEnd(',');
            if (week == "" || weekList.Count == 7)
                week = "*";
            return week;
        }

        public static TimeSpan getTriggerTime(HttpRequestBase request)
        {
            string TriggerTime = request.Form["TriggerTime"];
            TimeSpan ts = TimeSpan.Parse(TriggerTime);
            return ts;
        }
    }
}